package cn.xueden.system.service.impl;

import cn.xueden.system.mapper.RoleMapper;
import cn.xueden.system.model.entity.Role;
import cn.xueden.system.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author:梁志杰
 * @date:2025/6/10
 * @description:cn.xueden.system.service.impl
 * @version:1.0
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    /**
     * 分页获取角色列表
     * @param page
     * @return
     */
    @Override
    @Cacheable(value = "roleList",key = "#page.current+'_'+#page.size+'_'+#realName",
    unless = "#result == null || #result.records.isEmpty()" )
    public IPage<Role> getPage(Page<Role> page) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return baseMapper.selectPage(page,queryWrapper);
    }

    /**
     *  创建角色
     * @param role
     * @return
     */
    @Override
    @CacheEvict(value = "roleList",allEntries = true)
    public boolean createRole(Role role) {
        boolean result = baseMapper.insert(role)>0;
        return result;
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @Override
    @CacheEvict(value = "roleList",allEntries = true)
    public boolean updateRole(Role role) {
        if(baseMapper.updateById(role)>0){
            return true;
        }
        return false;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    @CacheEvict(value = "roleList",allEntries = true)
    public boolean deleteRole(Integer id) {
        if(baseMapper.deleteById(id)>0){
            return true;
        }
        return false;
    }
}
